// pages/EditProject/EditProject.js
const axios = require('../../utils/http.js');
const utils = require('../../utils/util.js');
const baseFn = utils.baseFn;
const formatTime = utils.formatTime
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    files: [],
    fliesLength:0,
    maxLength:200,
    startDate:'',
    endDate:'',
    proUpimgsArr: [],
    project: {
      user_id: app.globalData.userInfo.user_id,
      pro_name: "",
      pro_desc: "",
      pro_imgs: "",
      deadline: "",
      pre_reward: 0,
    },
    maxLength:200
  },
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      count:(4 - that.data.fliesLength),
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        // that.setData({
        //   files: that.data.files.concat(res.tempFilePaths)
        // });
        var _curTempFilePath = that.data.files;
        var tempFilePaths = res.tempFilePaths
        for (let i = 0; i < tempFilePaths.length;i++){
          wx.uploadFile({
            url: axios.urlR.baseUrl + axios.urlR.upLoad, //真实的接口地址
            filePath: tempFilePaths[i],
            name: 'file',
            formData: {},
            success: function (upres) {
              var _data = JSON.parse(upres.data)
              if (_data.code == 200){
                _curTempFilePath.push(_data.path);
                that.setData({ files: _curTempFilePath, fliesLength: _curTempFilePath.length, ['project.pro_imgs']: _curTempFilePath.join(',')})
                console.log(that.data)
              }else{
                axios.model('数据获取失败', _data.msg, false, undefined, undefined, function(){

                })
              }
              // console.log(data)
              //do something
            },
            complete:function(r){
            }
          })
        }
        // uploadTask.onProgressUpdate((res) => {
        //   console.log('上传进度', res.progress)
        //   console.log('已经上传的数据长度', res.totalBytesSent)
        //   console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
        // })
        // uploadTask.abort() // 取消上传任务
      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  proDescChange: function (e) {
    this.setData({
      ['project.pro_desc']: e.detail.value
    })
  },
  proNameChange:function(e){
    this.setData({
      ['project.pro_name']: e.detail.value
    })
  },
  proRewardChange: function (e) {
    this.setData({
      ['project.pre_reward']: e.detail.value
    })
  },
  editProjectClick: function(){
    var _proData = this.data.project
    var _txt=''
    if (!baseFn.notNull(_proData.pro_name)){
      _txt = "请输入项目标题";
    } else if (!baseFn.notNull(_proData.pro_desc)){
      _txt = "请输入项目描述";
    } else if (!baseFn.notNull(_proData.deadline)){
      _txt = "请选择截止日期";
    } else if (!baseFn.notNull(_proData.pre_reward)) {
      _txt = "请输入转发赏金";
    }
    // } else if (!baseFn.notNull(_proData.pro_imgs)) {
    //   _txt = "请选择相关图片";
    // }
    if (baseFn.notNull(_txt)){
      axios.model('输入有误', _txt,false, undefined,undefined,function(){},()=>{})
    }else{
      axios.POST(true, axios.urlR.proInfo, _proData, (res) => {
        // axios.model('发布成功'，)
        // axios.ti("成功", "success");
        wx.redirectTo({
          url: '../Transmit3/Transmit3?proId=' + res.pro_id + '&userId=' + this.data.project.user_id,
        })
      },()=>{})
      
    }     
  },
  deleteimg:function(v){
    var index = v.currentTarget.dataset.position;
    this.data.files.splice(index, 1);
    this.setData({
      files: this.data.files,
      fliesLength: this.data.files.length,
      ['project.pro_imgs']: this.data.files.join(',')
    });
    console.log(this.data)
  },
  bindDateChange: function (e) {
    var projectnew = this.data.project;
    projectnew.deadline = e.detail.value;
    this.setData({
      project: projectnew
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _startDate = formatTime(new Date(), 2);
    var _data = {
      startDate: _startDate,
      endDate: String(Number(_startDate.slice(0, 4)) + 5) + _startDate.slice(4),
      project: {
        user_id: app.globalData.userInfo.user_id,
        pro_name: "",
        pro_desc: "",
        pro_imgs: "",
        deadline: _startDate,
        pre_reward: 0,
      }
    }
    this.setData(_data) 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (res) {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function (res) {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})